package com.sky.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.mapper.CategoryMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    /**
     *     @Override
     * @param categoryPageQueryDTO
     * @return
     */
    public PageResult pageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {
        //select * from category limit ...

        //开始分页查询
        PageHelper.startPage(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());

        //调用基本查询功能，动态查询
        Page<Category> page = categoryMapper.pageQuery(categoryPageQueryDTO);

        //获取总数据数
        long total = page.getTotal();

        //获取查询数据
        List<Category> records = page.getResult();

        return new PageResult(total,records);
    }

    /**
     * 新增分类
     * @param categoryDTO
     */
    public void insertCategory(CategoryDTO categoryDTO) {
        Category category = new Category();

        BeanUtils.copyProperties(categoryDTO,category);

        //分类莫默认状态使0，禁用0，启用1
        category.setStatus(0);

        categoryMapper.insert(category);
    }

    /**
     * 修改分类状态，采用动态update sql，用于全局的分类修改
      * @param status
     * @param id
     */
    public void updateStatus(Integer status, Long id) {

        /*Category category = new Category();
        category.setStatus(status);
        category.setId(id);*/

        Category category = Category.builder()
                .id(id)
                .status(status).build();

        categoryMapper.update(category);
    }


    /**
     * 修改分类信息
     * @param categoryDTO
     */
    public void updateMes(CategoryDTO categoryDTO) {
        //封装category对象用于update sql
        Category category = new Category();

        BeanUtils.copyProperties(categoryDTO,category);

        categoryMapper.update(category);
    }


    /**
     * 删除分类
     * @param id
     */
    public void delete(long id) {
        //delete from table where id = id
        //删除数据一定要加条件，否则将会删除所有数据
        categoryMapper.delete(id);
    }


    /**
     * 根据类型查询
     * @param type
     * @return
     */
    public List<Category> selectByType(Integer type) {

        List<Category> categoryList = categoryMapper.selectByType(type);

        return categoryList;
    }
}
